/**
    Copyright (C) 2011  Alexander Vollmer

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.jefb.util;

import java.io.File;
import java.util.Random;

public class JefbUtils {
	public static final String FILE_SEPARATOR = System
			.getProperty("file.separator");
	private static String digits = "0123456789abcdef";

	public static boolean isEmptyString(String str) {
		if (str == null) {
			return true;
		}
		return str.isEmpty();
	}

	public static String extractRelativePath(String absolutePath, String homeDir) {
		if (JefbUtils.FILE_SEPARATOR.equals(homeDir)) {
			return absolutePath;
		} else {
			return absolutePath.replace(homeDir, "");
		}
	}

	public static String extractPath(File file) {
		String absolutePath = file.getAbsolutePath();
		return absolutePath.substring(0,
				absolutePath.lastIndexOf(file.getName()) - 1);
	}

	public static String extractExtension(File file) {
		String fileName = file.getName();
		if (fileName.contains(".")) {
			return fileName.substring(fileName.lastIndexOf("."),
					fileName.length());
		} else {
			return "";
		}
	}

	public static String extractExtension(String fileName) {
		if (fileName.contains(".")) {
			return fileName.substring(fileName.lastIndexOf('.'));
		} else
			return "";
	}

	public static String extractFileNameWithoutExtension(File file) {
		String fileName = file.getName();
		if (fileName.contains(".")) {
			return fileName.substring(0, fileName.lastIndexOf("."));
		} else {
			return fileName;
		}
	}

	public static String extractPath(String absoluteFileName, String fileName,
			String workspace, String baseDir) {
		String absolutePath = absoluteFileName.substring(0,
				absoluteFileName.lastIndexOf(fileName));
		String subPath = absolutePath.replace(baseDir, "");
		if (subPath.endsWith(FILE_SEPARATOR)) {
			return subPath.substring(0, subPath.lastIndexOf(FILE_SEPARATOR))
					.replace(workspace, "");
		} else
			return subPath.replace(workspace, "");
	}

	/**
	 * Return length many bytes of the passed in byte array as a hex string.
	 * 
	 * @param data
	 *            the bytes to be converted.
	 * @param length
	 *            the number of bytes in the data block to be converted.
	 * @return a hex representation of length bytes of data.
	 */
	public static String toHex(byte[] data, int length) {
		StringBuffer buf = new StringBuffer();
		for (int i = 0; i != length; i++) {
			int v = data[i] & 0xff;
			buf.append(digits.charAt(v >> 4));
			buf.append(digits.charAt(v & 0xf));
		}
		return buf.toString();
	}

	/**
	 * Return the passed in byte array as a hex string.
	 * 
	 * @param data
	 *            the bytes to be converted.
	 * @return a hex representation of data.
	 */
	public static String toHex(byte[] data) {
		return toHex(data, data.length);
	}

	public static String bytesToString(byte[] rowBytes) {
		StringBuilder sb = new StringBuilder();
		for (byte b : rowBytes) {
			sb.append((char) b);
		}
		return sb.toString();
	}

	private static char[] letters = new char[] { 'a', 'b', 'c', 'd', 'e', 'f',
			'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
			't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F',
			'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
			'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '1', '2', '3', '4', '5', '6',
			'7', '8', '9', '_', '-', '=' };

	public static String generateSessionId() {
		StringBuilder sb = new StringBuilder();
		Random random = new Random();
		for (int k = 0; k <= 3; k++) {
			for (int i = 0; i < 10; i++) {
				sb.append(letters[random.nextInt(letters.length - 1)]);
			}
			sb.append(".");
		}
		return sb.toString();
	}
}
